package stress;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import oldcode.HelloNettyClient;

public class HelloServiceStress {

	public static String host = "192.168.3.147";
	public static int port = 30000;
	
//	public static String host = "192.168.3.19";
//	public static int port = 30000;
	
//	public static String host = "192.168.3.147";
//	public static int port = 11111;
	
	public static int timeout = Integer.MAX_VALUE;
	
	public static int mthread = 3;
	
	public static int workload = 10000;
	
	public static class MyThread implements Runnable
	{
		public int id;
		
		@Override
		public void run() {
			
			HelloNettyClient c = new HelloNettyClient(host, port);
			
			c.send(workload);
			
			System.out.println("thread " + id + " finish!");
		}
		
	}
	

	
	public static void main(String[] args) {

		ExecutorService executor = Executors.newCachedThreadPool();
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < mthread; i++) 
		{
			MyThread thread = new MyThread();
			
			thread.id = i;
			
			executor.execute(thread);
		}
		executor.shutdown();
		try {
			executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long time = System.currentTimeMillis() - startTime;
		
		System.out.println("total time: " + time + ", total q: " + (mthread * workload) + ", total qps:" + ((mthread * workload * 1000)/time));
		
	}

}
